COMP4702 Assignment - Drosophila (Fruit Fly) Sex Classifier¶

Jack Barton SNo.46466293

Data Loading and Pre-processing¶

In [ ]:
import pandas as pd
from sklearn.impute import SimpleImputer
import seaborn as sns
import numpy as np

The data used was provided by: Hoffmann, Ary A.; Smith, Ailie; Griffin, Philippa C.; Hangartner, Sandra B. (2016). Data from: A collection of Australian Drosophila datasets on climate adaptation and species distributions [Dataset]. Dryad. https://doi.org/10.5061/dryad.k9c31

Load the data into a pandas DataFrame and visualize the data

In [ ]:
# Load the data into a pandas DataFrame
df = pd.read_csv("Data/83_Loeschcke_et_al_2000_Thorax_&_wing_traits_lab pops.csv")
# df = pd.read_csv("Data/84_Loeschcke_et_al_2000_Wing_traits_&_asymmetry_lab pops.csv")
# df = pd.read_csv("Data/85_Loeschcke_et_al_2000_Wing_asymmetry_lab_pops.csv")

# Print feature labels
print(df.columns.tolist())

# Plot the data against different features comparing the distribution of the 'Sex' feature
sns.pairplot(df, hue="Species")
sns.pairplot(df, hue="Population")
sns.pairplot(df, hue="Temperature")
sns.pairplot(df, hue="Sex")
['Species', 'Population', 'Latitude', 'Longitude', 'Year_start', 'Year_end', 'Temperature', 'Vial', 'Replicate', 'Sex', 'Thorax_length', 'l2', 'l3p', 'l3d', 'lpd', 'l3', 'w1', 'w2', 'w3', 'wing_loading']
Out[ ]:
<seaborn.axisgrid.PairGrid at 0x7f3bca269de0>
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image

Choosing the Feature to Classify¶

Choosing the Label/Feature to classify is an important step. In this case 'Sex' was chosen as it has the most separable data across all the features (sex is split the best in most features).

In [ ]:
# Define the label the model will classify
Label_name = 'Sex'

# Move the classifier column to the zeroth index
col = df.pop(Label_name)
df.insert(0, Label_name, col)

Removing Unproductive Features¶

Columns with only one value give no information to out model so passing them to the model is wasteful

In [ ]:
# Find columns of all the same value
all_same_value = df.nunique() == 1

# Print columns with all the same value
print("Columns with all the same value:")
print(all_same_value[all_same_value].index.tolist())
Columns with all the same value:
['Year_start', 'Year_end']

Reading the report for the data it can be found that 'Latitude' and 'Longitude' provide the same information as the 'Population' feature meaning if we keep the 'Population' feature it is wasteful to also add 'Latitude' and 'Longitude'. Additionally, 'Vial' was assumed to represent the batch in which they were collected, and 'Replicate' as assumed to represent generation of the batch but it wasn't confirmed and thus it was found best to avoid using data that may or may not affect the generalization of the model on future data.

In [ ]:
# List of columns to drop
columns_to_drop = ['Population','Latitude','Longitude','Vial', 'Replicate']
columns_to_drop.extend(all_same_value[all_same_value].index.tolist())

# Delete unnecessary columns
df.drop(columns=columns_to_drop, inplace=True)

# Print data labels
print(df.columns.tolist())
['Sex', 'Species', 'Temperature', 'Thorax_length', 'l2', 'l3p', 'l3d', 'lpd', 'l3', 'w1', 'w2', 'w3', 'wing_loading']

Removing/Repairing Damaged Data-Points¶

In [ ]:
from sklearn.preprocessing import LabelEncoder

Some rows are missing data entries, we have to decide if there is entries to be a valid point or if it isn't helpful enough and should just be deleted. therefore a threshold is assigned, if 70% of the data for the row is populated then it will be kept and less than 70% will be deleted from the data set to avoid bias data.

In [ ]:
# Count non-null values per row
non_null_counts = df.notnull().sum(axis=1)

# Remove rows with more than 3 data entries missing
threshold = df.shape[1] - 3

# Filter rows based on the threshold
df = df[non_null_counts >= threshold]

Now there are still rows missing up to 30% of their data entries to fix this we can impute the missing entries by filling numerical columns with the mean value of the column, and filling categorical columns with the most frequent value in their column.

In [ ]:
# Initialize SimpleImputers with appropriate strategies
fill_most_frequent = SimpleImputer(missing_values=np.nan, strategy="most_frequent")
fill_mean = SimpleImputer(missing_values=np.nan, strategy="mean")

Label_encoder_classifier = LabelEncoder()

# Iterate over columns in DataFrame
for column in df.columns:
    if isinstance(df[column][1], str):
        # For categorical columns, fill with most frequent value
        df[column] = pd.DataFrame(fill_most_frequent.fit_transform(df[[column]]))
        if df[column][1] == df[Label_name][1]:
            df[column] = pd.DataFrame(Label_encoder_classifier.fit_transform(df[column]))
        else:
            df[column] = pd.DataFrame(LabelEncoder().fit_transform(df[column]))
    else:
        # For numeric columns, fill with mean value
        df[column] = pd.DataFrame(fill_mean.fit_transform(df[[column]]))  # Fill missing values with mean

Visualizing the Pre-Processed Dataset¶

In [ ]:
sns.pairplot(df, hue=Label_name)
Out[ ]:
<seaborn.axisgrid.PairGrid at 0x7f3bc9489360>
No description has been provided for this image
In [ ]:
# Save the processed data
df.to_csv("Data/83_modified.csv", index=False)

Splitting the Dataset¶

In [ ]:
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder

When training a model it is important to split the dataset into training, validation, and test data. The training data is what is propagated through the model to update the model parameters. The validation data is passed through the model at the end of each epoch and gives a good estimate of the models generalization accuracy throughout training. Finally the test data is passed through the model after training to get the final un-bias model accuracy. The rough standard for the train-validation-test dataset split is 60-80% training data, 10-20% validation data, and 10-20% test data.

In [ ]:
# Assuming your dataframe df has the first column as the true labels (targets)
labels = df.iloc[:, 0].values  # Convert to numpy array
df = df.iloc[:, 1:].values   # Convert to numpy array

# Define ration of train, validation, and test data split
train_ratio = 0.7
val_ratio = 0.15
test_ratio = 0.15

# First, split into training and the rest
train_data, test_val_data, train_labels, test_val_labels = train_test_split(df, labels, test_size=1 - train_ratio, random_state=42)

# Second, split test_val_data into validation and test
val_data, test_data, val_labels, test_labels = train_test_split(test_val_data, test_val_labels, test_size=test_ratio/(test_ratio + val_ratio), random_state=42)

# Now you have train_data, val_data, and test_data ready
print(f"Training data size: {len(train_data)}")
print(f"Validation data size: {len(val_data)}")
print(f"Test data size: {len(test_data)}")
Training data size: 1211
Validation data size: 260
Test data size: 260

Model Creation¶

In [ ]:
import sys
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, TensorDataset
from torch.optim.lr_scheduler import StepLR
from typing import List
print(torch.__version__)
print("Cuda Available : {0}".format(torch.cuda.is_available()))
if torch.cuda.is_available():
    print('GPU - {0}'.format(torch.cuda.get_device_name()))
print("Python version:", sys.version)
2.3.0+cu121
Cuda Available : False
Python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

MLP¶

In [ ]:
# Define a simple MLP model constructor
class MLP(nn.Module):
    def __init__(self, dim_in: int, dim_out: int, hidden_layer_sizes: List[int]):
        super(MLP, self).__init__()
        
        print("Input Dimension: {0}".format(dim_in))
        print("Output Dimension: {0}".format(dim_out))
        print("Number of Hidden Layers: {0}".format(len(hidden_layer_sizes)))
        
        hiddens = [dim_in, *hidden_layer_sizes]
        layers = []
        
        # Create hidden layers with ReLU activation
        for i in range(len(hiddens) - 1):
            layers.append(nn.Linear(hiddens[i], hiddens[i + 1]))
            layers.append(nn.ReLU())
        
        # Add output layer
        if dim_out == 1:
            layers.append(nn.Linear(hiddens[-1], dim_out))
            layers.append(nn.Sigmoid())
        else:
            layers.append(nn.Linear(hiddens[-1], dim_out))
        
        self.model = nn.Sequential(*layers)
    
    def forward(self, x):
        return self.model(x)

Tunning the hidden layers of an MLP are very important. There are two aspects of hidden layers, size of the layer, and number of layers, these can be mixed and matched to provide the optimal model for the dataset.

Larger layers can capture more complex relationships within the data, but can lead to overfilling.

More layers can allow the model to learn more abstract features in higher dimensions improving generalization, but can lead to higher computational complexity.

In [ ]:
hidden_layers = [512,512]

# Initialize model
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = MLP(df.shape[1], 1, hidden_layers).to(device)
print(model)
Input Dimension: 12
Output Dimension: 1
Number of Hidden Layers: 2
MLP(
  (model): Sequential(
    (0): Linear(in_features=12, out_features=512, bias=True)
    (1): ReLU()
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): ReLU()
    (4): Linear(in_features=512, out_features=1, bias=True)
    (5): Sigmoid()
  )
)

Choosing Hyper Parameters¶

Choosing hyper parameters is one of the most important steps in developing a NN model. Tunning these values is the difference between having a model that gets 99% accuracy and a model that doesn't converge at all. The main hyper parameters to worry about include:

  • Epochs¶

  • Batch Size
  • Learning Rate
  • Loss Function
  • Optimiser (& optimiser specific hyper parameters)
In [ ]:
num_epochs = 500
batch_size = 128
learning_rate = 0.001
momentum = 0.9
alpha = 0.9
weight_decay = 0.01
loss_function = torch.nn.BCELoss()
# optimiser = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum)
# optimiser = torch.optim.Adagrad(model.parameters(), lr=learning_rate)
# optimiser = torch.optim.RMSprop(model.parameters(), lr=learning_rate, alpha=alpha)
optimiser = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)

scheduler = StepLR(optimiser, step_size=10, gamma=0.1) # Decreases Learning rate over epochs to refine loss.

Transforming the Data for Training¶

The datasets need to be converted to tensors for more efficient computation, then put into a DataLoader structure for batching and augmentation if necessary. This is the format in which pytorch passes data into a model.

In [ ]:
# Convert to tensors
train_data = torch.tensor(train_data, dtype=torch.float32)
train_labels = torch.tensor(train_labels, dtype=torch.long)
val_data = torch.tensor(val_data, dtype=torch.float32)
val_labels = torch.tensor(val_labels, dtype=torch.long)
test_data = torch.tensor(test_data, dtype=torch.float32)
test_labels = torch.tensor(test_labels, dtype=torch.long)

# Create TensorDatasets
train_dataset = TensorDataset(train_data, train_labels)
val_dataset = TensorDataset(val_data, val_labels)
test_dataset = TensorDataset(test_data, test_labels)

# Define dataloaders
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)

Training¶

In [ ]:
from tqdm import tqdm
import time
In [ ]:
# Lists to store the loss and accuracy values
train_losses = []
train_accuracies = []
val_accuracies = []
val_losses = []

# Start the timer
start_time = time.time()

# Training loop
for epoch in range(num_epochs):
  
  model.train()
  correct = 0
  total = 0
  running_loss = 0
  # get the inputs from train data loader
  for batch_idx, (df, targets) in enumerate(tqdm(train_loader, desc = f"Epoch {epoch+1}")):
    
    # zero the parameter gradients
    optimiser.zero_grad()
    # forward pass through the model
    outputs = model(df.to(device)).squeeze()
    # backward propagation + optimize
    targets = targets.float().to(device)
    loss = loss_function(outputs, targets)
    loss.backward()
    optimiser.step()
    
    predicted = (outputs > 0.5).float().to(device)
    total += targets.size(0)
    correct += (predicted == targets.to(device)).sum().item()
    running_loss += loss.item()
  
  # Calculate training stats
  epoch_loss = running_loss / len(train_loader)
  epoch_accuracy = 100 * correct / total
  train_losses.append(epoch_loss)
  train_accuracies.append(epoch_accuracy)

  # Validate the model
  model.eval()
  correct = 0
  total = 0
  val_loss = 0
  with torch.no_grad():
    
    # get inputs from validation data loader
    for df,targets in val_loader:
      
      # forward pass + get predicted outputs
      outputs = model(df.to(device)).squeeze()
      targets = targets.float().to(device)
      
      # Calculate validation loss
      loss = loss_function(outputs, targets.to(device))
      val_loss += loss.item() * df.size(0)
      
      predicted = (outputs > 0.5).float().cpu()
      total += targets.size(0)
      correct += (predicted == targets.cpu()).sum().item()
      
  val_epoch_loss = val_loss / len(val_loader.dataset)
  val_losses.append(val_epoch_loss)
      
  # calculate accuracy of predictions
  val_accuracy = 100 * correct / total
  val_accuracies.append(val_accuracy)
  
  # Reduce learning rate if converged
  if val_epoch_loss < 0.1:
    scheduler.step()

# Calculate the training time
training_time = time.time() - start_time
Epoch 1: 100%|██████████| 10/10 [00:00<00:00, 46.47it/s]
Epoch 2: 100%|██████████| 10/10 [00:00<00:00, 223.81it/s]
Epoch 3: 100%|██████████| 10/10 [00:00<00:00, 209.31it/s]
Epoch 4: 100%|██████████| 10/10 [00:00<00:00, 221.24it/s]
Epoch 5: 100%|██████████| 10/10 [00:00<00:00, 247.54it/s]
Epoch 6: 100%|██████████| 10/10 [00:00<00:00, 242.43it/s]
Epoch 7: 100%|██████████| 10/10 [00:00<00:00, 249.99it/s]
Epoch 8: 100%|██████████| 10/10 [00:00<00:00, 229.78it/s]
Epoch 9: 100%|██████████| 10/10 [00:00<00:00, 233.73it/s]
Epoch 10: 100%|██████████| 10/10 [00:00<00:00, 258.38it/s]
Epoch 11: 100%|██████████| 10/10 [00:00<00:00, 268.00it/s]
Epoch 12: 100%|██████████| 10/10 [00:00<00:00, 277.21it/s]
Epoch 13: 100%|██████████| 10/10 [00:00<00:00, 243.97it/s]
Epoch 14: 100%|██████████| 10/10 [00:00<00:00, 247.28it/s]
Epoch 15: 100%|██████████| 10/10 [00:00<00:00, 262.10it/s]
Epoch 16: 100%|██████████| 10/10 [00:00<00:00, 229.52it/s]
Epoch 17: 100%|██████████| 10/10 [00:00<00:00, 220.83it/s]
Epoch 18: 100%|██████████| 10/10 [00:00<00:00, 223.14it/s]
Epoch 19: 100%|██████████| 10/10 [00:00<00:00, 219.78it/s]
Epoch 20: 100%|██████████| 10/10 [00:00<00:00, 270.15it/s]
Epoch 21: 100%|██████████| 10/10 [00:00<00:00, 302.75it/s]
Epoch 22: 100%|██████████| 10/10 [00:00<00:00, 216.52it/s]
Epoch 23: 100%|██████████| 10/10 [00:00<00:00, 232.45it/s]
Epoch 24: 100%|██████████| 10/10 [00:00<00:00, 244.81it/s]
Epoch 25: 100%|██████████| 10/10 [00:00<00:00, 245.88it/s]
Epoch 26: 100%|██████████| 10/10 [00:00<00:00, 266.69it/s]
Epoch 27: 100%|██████████| 10/10 [00:00<00:00, 238.32it/s]
Epoch 28: 100%|██████████| 10/10 [00:00<00:00, 283.00it/s]
Epoch 29: 100%|██████████| 10/10 [00:00<00:00, 274.04it/s]
Epoch 30: 100%|██████████| 10/10 [00:00<00:00, 284.57it/s]
Epoch 31: 100%|██████████| 10/10 [00:00<00:00, 258.11it/s]
Epoch 32: 100%|██████████| 10/10 [00:00<00:00, 256.50it/s]
Epoch 33: 100%|██████████| 10/10 [00:00<00:00, 258.50it/s]
Epoch 34: 100%|██████████| 10/10 [00:00<00:00, 266.54it/s]
Epoch 35: 100%|██████████| 10/10 [00:00<00:00, 237.89it/s]
Epoch 36: 100%|██████████| 10/10 [00:00<00:00, 244.64it/s]
Epoch 37: 100%|██████████| 10/10 [00:00<00:00, 198.48it/s]
Epoch 38: 100%|██████████| 10/10 [00:00<00:00, 215.61it/s]
Epoch 39: 100%|██████████| 10/10 [00:00<00:00, 241.62it/s]
Epoch 40: 100%|██████████| 10/10 [00:00<00:00, 241.68it/s]
Epoch 41: 100%|██████████| 10/10 [00:00<00:00, 236.88it/s]
Epoch 42: 100%|██████████| 10/10 [00:00<00:00, 228.23it/s]
Epoch 43: 100%|██████████| 10/10 [00:00<00:00, 222.79it/s]
Epoch 44: 100%|██████████| 10/10 [00:00<00:00, 213.20it/s]
Epoch 45: 100%|██████████| 10/10 [00:00<00:00, 208.98it/s]
Epoch 46: 100%|██████████| 10/10 [00:00<00:00, 219.13it/s]
Epoch 47: 100%|██████████| 10/10 [00:00<00:00, 221.65it/s]
Epoch 48: 100%|██████████| 10/10 [00:00<00:00, 227.69it/s]
Epoch 49: 100%|██████████| 10/10 [00:00<00:00, 232.54it/s]
Epoch 50: 100%|██████████| 10/10 [00:00<00:00, 259.36it/s]
Epoch 51: 100%|██████████| 10/10 [00:00<00:00, 243.95it/s]
Epoch 52: 100%|██████████| 10/10 [00:00<00:00, 247.94it/s]
Epoch 53: 100%|██████████| 10/10 [00:00<00:00, 259.69it/s]
Epoch 54: 100%|██████████| 10/10 [00:00<00:00, 230.20it/s]
Epoch 55: 100%|██████████| 10/10 [00:00<00:00, 241.12it/s]
Epoch 56: 100%|██████████| 10/10 [00:00<00:00, 221.77it/s]
Epoch 57: 100%|██████████| 10/10 [00:00<00:00, 231.38it/s]
Epoch 58: 100%|██████████| 10/10 [00:00<00:00, 186.24it/s]
Epoch 59: 100%|██████████| 10/10 [00:00<00:00, 244.61it/s]
Epoch 60: 100%|██████████| 10/10 [00:00<00:00, 264.30it/s]
Epoch 61: 100%|██████████| 10/10 [00:00<00:00, 273.53it/s]
Epoch 62: 100%|██████████| 10/10 [00:00<00:00, 224.99it/s]
Epoch 63: 100%|██████████| 10/10 [00:00<00:00, 208.52it/s]
Epoch 64: 100%|██████████| 10/10 [00:00<00:00, 217.05it/s]
Epoch 65: 100%|██████████| 10/10 [00:00<00:00, 255.14it/s]
Epoch 66: 100%|██████████| 10/10 [00:00<00:00, 57.99it/s]
Epoch 67: 100%|██████████| 10/10 [00:00<00:00, 228.90it/s]
Epoch 68: 100%|██████████| 10/10 [00:00<00:00, 173.20it/s]
Epoch 69: 100%|██████████| 10/10 [00:00<00:00, 252.62it/s]
Epoch 70: 100%|██████████| 10/10 [00:00<00:00, 244.99it/s]
Epoch 71: 100%|██████████| 10/10 [00:00<00:00, 249.06it/s]
Epoch 72: 100%|██████████| 10/10 [00:00<00:00, 241.37it/s]
Epoch 73: 100%|██████████| 10/10 [00:00<00:00, 239.39it/s]
Epoch 74: 100%|██████████| 10/10 [00:00<00:00, 250.00it/s]
Epoch 75: 100%|██████████| 10/10 [00:00<00:00, 223.36it/s]
Epoch 76: 100%|██████████| 10/10 [00:00<00:00, 203.75it/s]
Epoch 77: 100%|██████████| 10/10 [00:00<00:00, 238.00it/s]
Epoch 78: 100%|██████████| 10/10 [00:00<00:00, 216.88it/s]
Epoch 79: 100%|██████████| 10/10 [00:00<00:00, 198.31it/s]
Epoch 80: 100%|██████████| 10/10 [00:00<00:00, 231.09it/s]
Epoch 81: 100%|██████████| 10/10 [00:00<00:00, 258.05it/s]
Epoch 82: 100%|██████████| 10/10 [00:00<00:00, 243.29it/s]
Epoch 83: 100%|██████████| 10/10 [00:00<00:00, 212.54it/s]
Epoch 84: 100%|██████████| 10/10 [00:00<00:00, 268.62it/s]
Epoch 85: 100%|██████████| 10/10 [00:00<00:00, 270.92it/s]
Epoch 86: 100%|██████████| 10/10 [00:00<00:00, 251.06it/s]
Epoch 87: 100%|██████████| 10/10 [00:00<00:00, 285.59it/s]
Epoch 88: 100%|██████████| 10/10 [00:00<00:00, 238.14it/s]
Epoch 89: 100%|██████████| 10/10 [00:00<00:00, 251.69it/s]
Epoch 90: 100%|██████████| 10/10 [00:00<00:00, 255.84it/s]
Epoch 91: 100%|██████████| 10/10 [00:00<00:00, 267.85it/s]
Epoch 92: 100%|██████████| 10/10 [00:00<00:00, 213.75it/s]
Epoch 93: 100%|██████████| 10/10 [00:00<00:00, 208.23it/s]
Epoch 94: 100%|██████████| 10/10 [00:00<00:00, 243.56it/s]
Epoch 95: 100%|██████████| 10/10 [00:00<00:00, 267.46it/s]
Epoch 96: 100%|██████████| 10/10 [00:00<00:00, 228.06it/s]
Epoch 97: 100%|██████████| 10/10 [00:00<00:00, 236.42it/s]
Epoch 98: 100%|██████████| 10/10 [00:00<00:00, 236.47it/s]
Epoch 99: 100%|██████████| 10/10 [00:00<00:00, 217.33it/s]
Epoch 100: 100%|██████████| 10/10 [00:00<00:00, 235.52it/s]
Epoch 101: 100%|██████████| 10/10 [00:00<00:00, 199.15it/s]
Epoch 102: 100%|██████████| 10/10 [00:00<00:00, 276.40it/s]
Epoch 103: 100%|██████████| 10/10 [00:00<00:00, 244.25it/s]
Epoch 104: 100%|██████████| 10/10 [00:00<00:00, 247.77it/s]
Epoch 105: 100%|██████████| 10/10 [00:00<00:00, 223.02it/s]
Epoch 106: 100%|██████████| 10/10 [00:00<00:00, 260.53it/s]
Epoch 107: 100%|██████████| 10/10 [00:00<00:00, 266.12it/s]
Epoch 108: 100%|██████████| 10/10 [00:00<00:00, 265.30it/s]
Epoch 109: 100%|██████████| 10/10 [00:00<00:00, 254.68it/s]
Epoch 110: 100%|██████████| 10/10 [00:00<00:00, 280.66it/s]
Epoch 111: 100%|██████████| 10/10 [00:00<00:00, 251.19it/s]
Epoch 112: 100%|██████████| 10/10 [00:00<00:00, 253.24it/s]
Epoch 113: 100%|██████████| 10/10 [00:00<00:00, 290.04it/s]
Epoch 114: 100%|██████████| 10/10 [00:00<00:00, 237.59it/s]
Epoch 115: 100%|██████████| 10/10 [00:00<00:00, 247.25it/s]
Epoch 116: 100%|██████████| 10/10 [00:00<00:00, 247.66it/s]
Epoch 117: 100%|██████████| 10/10 [00:00<00:00, 280.99it/s]
Epoch 118: 100%|██████████| 10/10 [00:00<00:00, 262.24it/s]
Epoch 119: 100%|██████████| 10/10 [00:00<00:00, 266.25it/s]
Epoch 120: 100%|██████████| 10/10 [00:00<00:00, 262.84it/s]
Epoch 121: 100%|██████████| 10/10 [00:00<00:00, 229.70it/s]
Epoch 122: 100%|██████████| 10/10 [00:00<00:00, 234.04it/s]
Epoch 123: 100%|██████████| 10/10 [00:00<00:00, 222.14it/s]
Epoch 124: 100%|██████████| 10/10 [00:00<00:00, 241.99it/s]
Epoch 125: 100%|██████████| 10/10 [00:00<00:00, 279.12it/s]
Epoch 126: 100%|██████████| 10/10 [00:00<00:00, 250.12it/s]
Epoch 127: 100%|██████████| 10/10 [00:00<00:00, 246.22it/s]
Epoch 128: 100%|██████████| 10/10 [00:00<00:00, 232.04it/s]
Epoch 129: 100%|██████████| 10/10 [00:00<00:00, 243.16it/s]
Epoch 130: 100%|██████████| 10/10 [00:00<00:00, 262.52it/s]
Epoch 131: 100%|██████████| 10/10 [00:00<00:00, 238.96it/s]
Epoch 132: 100%|██████████| 10/10 [00:00<00:00, 239.14it/s]
Epoch 133: 100%|██████████| 10/10 [00:00<00:00, 255.26it/s]
Epoch 134: 100%|██████████| 10/10 [00:00<00:00, 242.96it/s]
Epoch 135: 100%|██████████| 10/10 [00:00<00:00, 273.03it/s]
Epoch 136: 100%|██████████| 10/10 [00:00<00:00, 257.40it/s]
Epoch 137: 100%|██████████| 10/10 [00:00<00:00, 258.84it/s]
Epoch 138: 100%|██████████| 10/10 [00:00<00:00, 257.54it/s]
Epoch 139: 100%|██████████| 10/10 [00:00<00:00, 258.63it/s]
Epoch 140: 100%|██████████| 10/10 [00:00<00:00, 290.45it/s]
Epoch 141: 100%|██████████| 10/10 [00:00<00:00, 266.14it/s]
Epoch 142: 100%|██████████| 10/10 [00:00<00:00, 228.47it/s]
Epoch 143: 100%|██████████| 10/10 [00:00<00:00, 245.92it/s]
Epoch 144: 100%|██████████| 10/10 [00:00<00:00, 261.07it/s]
Epoch 145: 100%|██████████| 10/10 [00:00<00:00, 203.58it/s]
Epoch 146: 100%|██████████| 10/10 [00:00<00:00, 216.42it/s]
Epoch 147: 100%|██████████| 10/10 [00:00<00:00, 230.12it/s]
Epoch 148: 100%|██████████| 10/10 [00:00<00:00, 227.00it/s]
Epoch 149: 100%|██████████| 10/10 [00:00<00:00, 246.59it/s]
Epoch 150: 100%|██████████| 10/10 [00:00<00:00, 235.86it/s]
Epoch 151: 100%|██████████| 10/10 [00:00<00:00, 218.11it/s]
Epoch 152: 100%|██████████| 10/10 [00:00<00:00, 234.83it/s]
Epoch 153: 100%|██████████| 10/10 [00:00<00:00, 246.67it/s]
Epoch 154: 100%|██████████| 10/10 [00:00<00:00, 289.67it/s]
Epoch 155: 100%|██████████| 10/10 [00:00<00:00, 233.74it/s]
Epoch 156: 100%|██████████| 10/10 [00:00<00:00, 210.22it/s]
Epoch 157: 100%|██████████| 10/10 [00:00<00:00, 238.44it/s]
Epoch 158: 100%|██████████| 10/10 [00:00<00:00, 245.87it/s]
Epoch 159: 100%|██████████| 10/10 [00:00<00:00, 257.17it/s]
Epoch 160: 100%|██████████| 10/10 [00:00<00:00, 253.40it/s]
Epoch 161: 100%|██████████| 10/10 [00:00<00:00, 250.14it/s]
Epoch 162: 100%|██████████| 10/10 [00:00<00:00, 252.17it/s]
Epoch 163: 100%|██████████| 10/10 [00:00<00:00, 232.12it/s]
Epoch 164: 100%|██████████| 10/10 [00:00<00:00, 255.16it/s]
Epoch 165: 100%|██████████| 10/10 [00:00<00:00, 253.43it/s]
Epoch 166: 100%|██████████| 10/10 [00:00<00:00, 264.12it/s]
Epoch 167: 100%|██████████| 10/10 [00:00<00:00, 261.73it/s]
Epoch 168: 100%|██████████| 10/10 [00:00<00:00, 240.17it/s]
Epoch 169: 100%|██████████| 10/10 [00:00<00:00, 236.98it/s]
Epoch 170: 100%|██████████| 10/10 [00:00<00:00, 259.13it/s]
Epoch 171: 100%|██████████| 10/10 [00:00<00:00, 249.48it/s]
Epoch 172: 100%|██████████| 10/10 [00:00<00:00, 279.93it/s]
Epoch 173: 100%|██████████| 10/10 [00:00<00:00, 267.46it/s]
Epoch 174: 100%|██████████| 10/10 [00:00<00:00, 249.93it/s]
Epoch 175: 100%|██████████| 10/10 [00:00<00:00, 263.24it/s]
Epoch 176: 100%|██████████| 10/10 [00:00<00:00, 242.93it/s]
Epoch 177: 100%|██████████| 10/10 [00:00<00:00, 238.75it/s]
Epoch 178: 100%|██████████| 10/10 [00:00<00:00, 281.99it/s]
Epoch 179: 100%|██████████| 10/10 [00:00<00:00, 240.84it/s]
Epoch 180: 100%|██████████| 10/10 [00:00<00:00, 252.96it/s]
Epoch 181: 100%|██████████| 10/10 [00:00<00:00, 251.48it/s]
Epoch 182: 100%|██████████| 10/10 [00:00<00:00, 245.58it/s]
Epoch 183: 100%|██████████| 10/10 [00:00<00:00, 259.25it/s]
Epoch 184: 100%|██████████| 10/10 [00:00<00:00, 246.10it/s]
Epoch 185: 100%|██████████| 10/10 [00:00<00:00, 238.50it/s]
Epoch 186: 100%|██████████| 10/10 [00:00<00:00, 263.84it/s]
Epoch 187: 100%|██████████| 10/10 [00:00<00:00, 277.83it/s]
Epoch 188: 100%|██████████| 10/10 [00:00<00:00, 276.68it/s]
Epoch 189: 100%|██████████| 10/10 [00:00<00:00, 262.71it/s]
Epoch 190: 100%|██████████| 10/10 [00:00<00:00, 236.97it/s]
Epoch 191: 100%|██████████| 10/10 [00:00<00:00, 253.70it/s]
Epoch 192: 100%|██████████| 10/10 [00:00<00:00, 226.67it/s]
Epoch 193: 100%|██████████| 10/10 [00:00<00:00, 240.68it/s]
Epoch 194: 100%|██████████| 10/10 [00:00<00:00, 252.26it/s]
Epoch 195: 100%|██████████| 10/10 [00:00<00:00, 267.11it/s]
Epoch 196: 100%|██████████| 10/10 [00:00<00:00, 254.86it/s]
Epoch 197: 100%|██████████| 10/10 [00:00<00:00, 217.04it/s]
Epoch 198: 100%|██████████| 10/10 [00:00<00:00, 206.70it/s]
Epoch 199: 100%|██████████| 10/10 [00:00<00:00, 218.84it/s]
Epoch 200: 100%|██████████| 10/10 [00:00<00:00, 225.13it/s]
Epoch 201: 100%|██████████| 10/10 [00:00<00:00, 51.44it/s]
Epoch 202: 100%|██████████| 10/10 [00:00<00:00, 168.71it/s]
Epoch 203: 100%|██████████| 10/10 [00:00<00:00, 202.47it/s]
Epoch 204: 100%|██████████| 10/10 [00:00<00:00, 219.94it/s]
Epoch 205: 100%|██████████| 10/10 [00:00<00:00, 235.39it/s]
Epoch 206: 100%|██████████| 10/10 [00:00<00:00, 270.55it/s]
Epoch 207: 100%|██████████| 10/10 [00:00<00:00, 250.31it/s]
Epoch 208: 100%|██████████| 10/10 [00:00<00:00, 239.04it/s]
Epoch 209: 100%|██████████| 10/10 [00:00<00:00, 278.21it/s]
Epoch 210: 100%|██████████| 10/10 [00:00<00:00, 239.28it/s]
Epoch 211: 100%|██████████| 10/10 [00:00<00:00, 269.41it/s]
Epoch 212: 100%|██████████| 10/10 [00:00<00:00, 265.92it/s]
Epoch 213: 100%|██████████| 10/10 [00:00<00:00, 250.66it/s]
Epoch 214: 100%|██████████| 10/10 [00:00<00:00, 249.72it/s]
Epoch 215: 100%|██████████| 10/10 [00:00<00:00, 269.53it/s]
Epoch 216: 100%|██████████| 10/10 [00:00<00:00, 244.25it/s]
Epoch 217: 100%|██████████| 10/10 [00:00<00:00, 268.13it/s]
Epoch 218: 100%|██████████| 10/10 [00:00<00:00, 234.48it/s]
Epoch 219: 100%|██████████| 10/10 [00:00<00:00, 243.00it/s]
Epoch 220: 100%|██████████| 10/10 [00:00<00:00, 246.26it/s]
Epoch 221: 100%|██████████| 10/10 [00:00<00:00, 259.31it/s]
Epoch 222: 100%|██████████| 10/10 [00:00<00:00, 230.88it/s]
Epoch 223: 100%|██████████| 10/10 [00:00<00:00, 233.60it/s]
Epoch 224: 100%|██████████| 10/10 [00:00<00:00, 265.14it/s]
Epoch 225: 100%|██████████| 10/10 [00:00<00:00, 285.30it/s]
Epoch 226: 100%|██████████| 10/10 [00:00<00:00, 247.85it/s]
Epoch 227: 100%|██████████| 10/10 [00:00<00:00, 259.80it/s]
Epoch 228: 100%|██████████| 10/10 [00:00<00:00, 263.52it/s]
Epoch 229: 100%|██████████| 10/10 [00:00<00:00, 264.01it/s]
Epoch 230: 100%|██████████| 10/10 [00:00<00:00, 266.86it/s]
Epoch 231: 100%|██████████| 10/10 [00:00<00:00, 251.67it/s]
Epoch 232: 100%|██████████| 10/10 [00:00<00:00, 253.09it/s]
Epoch 233: 100%|██████████| 10/10 [00:00<00:00, 265.98it/s]
Epoch 234: 100%|██████████| 10/10 [00:00<00:00, 231.96it/s]
Epoch 235: 100%|██████████| 10/10 [00:00<00:00, 232.26it/s]
Epoch 236: 100%|██████████| 10/10 [00:00<00:00, 249.53it/s]
Epoch 237: 100%|██████████| 10/10 [00:00<00:00, 252.13it/s]
Epoch 238: 100%|██████████| 10/10 [00:00<00:00, 286.40it/s]
Epoch 239: 100%|██████████| 10/10 [00:00<00:00, 260.17it/s]
Epoch 240: 100%|██████████| 10/10 [00:00<00:00, 265.29it/s]
Epoch 241: 100%|██████████| 10/10 [00:00<00:00, 256.47it/s]
Epoch 242: 100%|██████████| 10/10 [00:00<00:00, 220.94it/s]
Epoch 243: 100%|██████████| 10/10 [00:00<00:00, 219.98it/s]
Epoch 244: 100%|██████████| 10/10 [00:00<00:00, 230.83it/s]
Epoch 245: 100%|██████████| 10/10 [00:00<00:00, 248.78it/s]
Epoch 246: 100%|██████████| 10/10 [00:00<00:00, 251.23it/s]
Epoch 247: 100%|██████████| 10/10 [00:00<00:00, 271.39it/s]
Epoch 248: 100%|██████████| 10/10 [00:00<00:00, 238.51it/s]
Epoch 249: 100%|██████████| 10/10 [00:00<00:00, 213.70it/s]
Epoch 250: 100%|██████████| 10/10 [00:00<00:00, 230.06it/s]
Epoch 251: 100%|██████████| 10/10 [00:00<00:00, 238.95it/s]
Epoch 252: 100%|██████████| 10/10 [00:00<00:00, 227.92it/s]
Epoch 253: 100%|██████████| 10/10 [00:00<00:00, 236.22it/s]
Epoch 254: 100%|██████████| 10/10 [00:00<00:00, 268.98it/s]
Epoch 255: 100%|██████████| 10/10 [00:00<00:00, 235.91it/s]
Epoch 256: 100%|██████████| 10/10 [00:00<00:00, 181.97it/s]
Epoch 257: 100%|██████████| 10/10 [00:00<00:00, 176.13it/s]
Epoch 258: 100%|██████████| 10/10 [00:00<00:00, 246.93it/s]
Epoch 259: 100%|██████████| 10/10 [00:00<00:00, 261.15it/s]
Epoch 260: 100%|██████████| 10/10 [00:00<00:00, 291.39it/s]
Epoch 261: 100%|██████████| 10/10 [00:00<00:00, 279.83it/s]
Epoch 262: 100%|██████████| 10/10 [00:00<00:00, 223.72it/s]
Epoch 263: 100%|██████████| 10/10 [00:00<00:00, 221.67it/s]
Epoch 264: 100%|██████████| 10/10 [00:00<00:00, 188.42it/s]
Epoch 265: 100%|██████████| 10/10 [00:00<00:00, 265.90it/s]
Epoch 266: 100%|██████████| 10/10 [00:00<00:00, 248.20it/s]
Epoch 267: 100%|██████████| 10/10 [00:00<00:00, 271.40it/s]
Epoch 268: 100%|██████████| 10/10 [00:00<00:00, 260.47it/s]
Epoch 269: 100%|██████████| 10/10 [00:00<00:00, 252.59it/s]
Epoch 270: 100%|██████████| 10/10 [00:00<00:00, 252.01it/s]
Epoch 271: 100%|██████████| 10/10 [00:00<00:00, 254.29it/s]
Epoch 272: 100%|██████████| 10/10 [00:00<00:00, 270.30it/s]
Epoch 273: 100%|██████████| 10/10 [00:00<00:00, 242.26it/s]
Epoch 274: 100%|██████████| 10/10 [00:00<00:00, 209.65it/s]
Epoch 275: 100%|██████████| 10/10 [00:00<00:00, 240.81it/s]
Epoch 276: 100%|██████████| 10/10 [00:00<00:00, 225.34it/s]
Epoch 277: 100%|██████████| 10/10 [00:00<00:00, 242.05it/s]
Epoch 278: 100%|██████████| 10/10 [00:00<00:00, 254.50it/s]
Epoch 279: 100%|██████████| 10/10 [00:00<00:00, 245.23it/s]
Epoch 280: 100%|██████████| 10/10 [00:00<00:00, 244.75it/s]
Epoch 281: 100%|██████████| 10/10 [00:00<00:00, 254.69it/s]
Epoch 282: 100%|██████████| 10/10 [00:00<00:00, 275.08it/s]
Epoch 283: 100%|██████████| 10/10 [00:00<00:00, 234.39it/s]
Epoch 284: 100%|██████████| 10/10 [00:00<00:00, 228.07it/s]
Epoch 285: 100%|██████████| 10/10 [00:00<00:00, 237.04it/s]
Epoch 286: 100%|██████████| 10/10 [00:00<00:00, 280.20it/s]
Epoch 287: 100%|██████████| 10/10 [00:00<00:00, 259.27it/s]
Epoch 288: 100%|██████████| 10/10 [00:00<00:00, 291.98it/s]
Epoch 289: 100%|██████████| 10/10 [00:00<00:00, 253.50it/s]
Epoch 290: 100%|██████████| 10/10 [00:00<00:00, 61.44it/s]
Epoch 291: 100%|██████████| 10/10 [00:00<00:00, 232.10it/s]
Epoch 292: 100%|██████████| 10/10 [00:00<00:00, 218.41it/s]
Epoch 293: 100%|██████████| 10/10 [00:00<00:00, 238.38it/s]
Epoch 294: 100%|██████████| 10/10 [00:00<00:00, 234.44it/s]
Epoch 295: 100%|██████████| 10/10 [00:00<00:00, 231.83it/s]
Epoch 296: 100%|██████████| 10/10 [00:00<00:00, 255.28it/s]
Epoch 297: 100%|██████████| 10/10 [00:00<00:00, 224.71it/s]
Epoch 298: 100%|██████████| 10/10 [00:00<00:00, 237.56it/s]
Epoch 299: 100%|██████████| 10/10 [00:00<00:00, 221.23it/s]
Epoch 300: 100%|██████████| 10/10 [00:00<00:00, 222.37it/s]
Epoch 301: 100%|██████████| 10/10 [00:00<00:00, 206.48it/s]
Epoch 302: 100%|██████████| 10/10 [00:00<00:00, 202.80it/s]
Epoch 303: 100%|██████████| 10/10 [00:00<00:00, 245.49it/s]
Epoch 304: 100%|██████████| 10/10 [00:00<00:00, 248.88it/s]
Epoch 305: 100%|██████████| 10/10 [00:00<00:00, 199.50it/s]
Epoch 306: 100%|██████████| 10/10 [00:00<00:00, 178.20it/s]
Epoch 307: 100%|██████████| 10/10 [00:00<00:00, 263.77it/s]
Epoch 308: 100%|██████████| 10/10 [00:00<00:00, 253.78it/s]
Epoch 309: 100%|██████████| 10/10 [00:00<00:00, 274.47it/s]
Epoch 310: 100%|██████████| 10/10 [00:00<00:00, 260.38it/s]
Epoch 311: 100%|██████████| 10/10 [00:00<00:00, 249.34it/s]
Epoch 312: 100%|██████████| 10/10 [00:00<00:00, 238.71it/s]
Epoch 313: 100%|██████████| 10/10 [00:00<00:00, 255.95it/s]
Epoch 314: 100%|██████████| 10/10 [00:00<00:00, 253.82it/s]
Epoch 315: 100%|██████████| 10/10 [00:00<00:00, 247.32it/s]
Epoch 316: 100%|██████████| 10/10 [00:00<00:00, 259.23it/s]
Epoch 317: 100%|██████████| 10/10 [00:00<00:00, 268.05it/s]
Epoch 318: 100%|██████████| 10/10 [00:00<00:00, 276.59it/s]
Epoch 319: 100%|██████████| 10/10 [00:00<00:00, 246.53it/s]
Epoch 320: 100%|██████████| 10/10 [00:00<00:00, 264.34it/s]
Epoch 321: 100%|██████████| 10/10 [00:00<00:00, 267.01it/s]
Epoch 322: 100%|██████████| 10/10 [00:00<00:00, 221.91it/s]
Epoch 323: 100%|██████████| 10/10 [00:00<00:00, 220.81it/s]
Epoch 324: 100%|██████████| 10/10 [00:00<00:00, 252.19it/s]
Epoch 325: 100%|██████████| 10/10 [00:00<00:00, 257.31it/s]
Epoch 326: 100%|██████████| 10/10 [00:00<00:00, 278.23it/s]
Epoch 327: 100%|██████████| 10/10 [00:00<00:00, 260.33it/s]
Epoch 328: 100%|██████████| 10/10 [00:00<00:00, 227.27it/s]
Epoch 329: 100%|██████████| 10/10 [00:00<00:00, 249.75it/s]
Epoch 330: 100%|██████████| 10/10 [00:00<00:00, 268.62it/s]
Epoch 331: 100%|██████████| 10/10 [00:00<00:00, 251.56it/s]
Epoch 332: 100%|██████████| 10/10 [00:00<00:00, 251.70it/s]
Epoch 333: 100%|██████████| 10/10 [00:00<00:00, 288.73it/s]
Epoch 334: 100%|██████████| 10/10 [00:00<00:00, 258.88it/s]
Epoch 335: 100%|██████████| 10/10 [00:00<00:00, 268.16it/s]
Epoch 336: 100%|██████████| 10/10 [00:00<00:00, 274.40it/s]
Epoch 337: 100%|██████████| 10/10 [00:00<00:00, 247.64it/s]
Epoch 338: 100%|██████████| 10/10 [00:00<00:00, 272.24it/s]
Epoch 339: 100%|██████████| 10/10 [00:00<00:00, 271.43it/s]
Epoch 340: 100%|██████████| 10/10 [00:00<00:00, 260.89it/s]
Epoch 341: 100%|██████████| 10/10 [00:00<00:00, 262.74it/s]
Epoch 342: 100%|██████████| 10/10 [00:00<00:00, 256.92it/s]
Epoch 343: 100%|██████████| 10/10 [00:00<00:00, 259.45it/s]
Epoch 344: 100%|██████████| 10/10 [00:00<00:00, 240.89it/s]
Epoch 345: 100%|██████████| 10/10 [00:00<00:00, 213.51it/s]
Epoch 346: 100%|██████████| 10/10 [00:00<00:00, 227.12it/s]
Epoch 347: 100%|██████████| 10/10 [00:00<00:00, 237.64it/s]
Epoch 348: 100%|██████████| 10/10 [00:00<00:00, 243.19it/s]
Epoch 349: 100%|██████████| 10/10 [00:00<00:00, 223.19it/s]
Epoch 350: 100%|██████████| 10/10 [00:00<00:00, 209.66it/s]
Epoch 351: 100%|██████████| 10/10 [00:00<00:00, 228.10it/s]
Epoch 352: 100%|██████████| 10/10 [00:00<00:00, 228.86it/s]
Epoch 353: 100%|██████████| 10/10 [00:00<00:00, 244.05it/s]
Epoch 354: 100%|██████████| 10/10 [00:00<00:00, 264.87it/s]
Epoch 355: 100%|██████████| 10/10 [00:00<00:00, 235.20it/s]
Epoch 356: 100%|██████████| 10/10 [00:00<00:00, 243.96it/s]
Epoch 357: 100%|██████████| 10/10 [00:00<00:00, 236.27it/s]
Epoch 358: 100%|██████████| 10/10 [00:00<00:00, 251.98it/s]
Epoch 359: 100%|██████████| 10/10 [00:00<00:00, 241.42it/s]
Epoch 360: 100%|██████████| 10/10 [00:00<00:00, 249.19it/s]
Epoch 361: 100%|██████████| 10/10 [00:00<00:00, 58.01it/s]
Epoch 362: 100%|██████████| 10/10 [00:00<00:00, 194.09it/s]
Epoch 363: 100%|██████████| 10/10 [00:00<00:00, 208.82it/s]
Epoch 364: 100%|██████████| 10/10 [00:00<00:00, 236.20it/s]
Epoch 365: 100%|██████████| 10/10 [00:00<00:00, 219.21it/s]
Epoch 366: 100%|██████████| 10/10 [00:00<00:00, 255.38it/s]
Epoch 367: 100%|██████████| 10/10 [00:00<00:00, 252.38it/s]
Epoch 368: 100%|██████████| 10/10 [00:00<00:00, 261.60it/s]
Epoch 369: 100%|██████████| 10/10 [00:00<00:00, 233.89it/s]
Epoch 370: 100%|██████████| 10/10 [00:00<00:00, 211.18it/s]
Epoch 371: 100%|██████████| 10/10 [00:00<00:00, 244.18it/s]
Epoch 372: 100%|██████████| 10/10 [00:00<00:00, 229.48it/s]
Epoch 373: 100%|██████████| 10/10 [00:00<00:00, 221.75it/s]
Epoch 374: 100%|██████████| 10/10 [00:00<00:00, 192.87it/s]
Epoch 375: 100%|██████████| 10/10 [00:00<00:00, 243.56it/s]
Epoch 376: 100%|██████████| 10/10 [00:00<00:00, 240.13it/s]
Epoch 377: 100%|██████████| 10/10 [00:00<00:00, 224.14it/s]
Epoch 378: 100%|██████████| 10/10 [00:00<00:00, 225.62it/s]
Epoch 379: 100%|██████████| 10/10 [00:00<00:00, 264.72it/s]
Epoch 380: 100%|██████████| 10/10 [00:00<00:00, 218.72it/s]
Epoch 381: 100%|██████████| 10/10 [00:00<00:00, 235.22it/s]
Epoch 382: 100%|██████████| 10/10 [00:00<00:00, 208.39it/s]
Epoch 383: 100%|██████████| 10/10 [00:00<00:00, 277.68it/s]
Epoch 384: 100%|██████████| 10/10 [00:00<00:00, 262.37it/s]
Epoch 385: 100%|██████████| 10/10 [00:00<00:00, 266.02it/s]
Epoch 386: 100%|██████████| 10/10 [00:00<00:00, 217.21it/s]
Epoch 387: 100%|██████████| 10/10 [00:00<00:00, 246.82it/s]
Epoch 388: 100%|██████████| 10/10 [00:00<00:00, 249.05it/s]
Epoch 389: 100%|██████████| 10/10 [00:00<00:00, 258.73it/s]
Epoch 390: 100%|██████████| 10/10 [00:00<00:00, 256.90it/s]
Epoch 391: 100%|██████████| 10/10 [00:00<00:00, 251.25it/s]
Epoch 392: 100%|██████████| 10/10 [00:00<00:00, 233.80it/s]
Epoch 393: 100%|██████████| 10/10 [00:00<00:00, 246.82it/s]
Epoch 394: 100%|██████████| 10/10 [00:00<00:00, 262.85it/s]
Epoch 395: 100%|██████████| 10/10 [00:00<00:00, 242.29it/s]
Epoch 396: 100%|██████████| 10/10 [00:00<00:00, 287.79it/s]
Epoch 397: 100%|██████████| 10/10 [00:00<00:00, 263.65it/s]
Epoch 398: 100%|██████████| 10/10 [00:00<00:00, 215.53it/s]
Epoch 399: 100%|██████████| 10/10 [00:00<00:00, 232.75it/s]
Epoch 400: 100%|██████████| 10/10 [00:00<00:00, 240.20it/s]
Epoch 401: 100%|██████████| 10/10 [00:00<00:00, 215.08it/s]
Epoch 402: 100%|██████████| 10/10 [00:00<00:00, 218.68it/s]
Epoch 403: 100%|██████████| 10/10 [00:00<00:00, 221.42it/s]
Epoch 404: 100%|██████████| 10/10 [00:00<00:00, 241.33it/s]
Epoch 405: 100%|██████████| 10/10 [00:00<00:00, 221.45it/s]
Epoch 406: 100%|██████████| 10/10 [00:00<00:00, 243.83it/s]
Epoch 407: 100%|██████████| 10/10 [00:00<00:00, 250.68it/s]
Epoch 408: 100%|██████████| 10/10 [00:00<00:00, 226.70it/s]
Epoch 409: 100%|██████████| 10/10 [00:00<00:00, 245.69it/s]
Epoch 410: 100%|██████████| 10/10 [00:00<00:00, 243.77it/s]
Epoch 411: 100%|██████████| 10/10 [00:00<00:00, 253.92it/s]
Epoch 412: 100%|██████████| 10/10 [00:00<00:00, 249.32it/s]
Epoch 413: 100%|██████████| 10/10 [00:00<00:00, 260.11it/s]
Epoch 414: 100%|██████████| 10/10 [00:00<00:00, 230.15it/s]
Epoch 415: 100%|██████████| 10/10 [00:00<00:00, 211.06it/s]
Epoch 416: 100%|██████████| 10/10 [00:00<00:00, 244.70it/s]
Epoch 417: 100%|██████████| 10/10 [00:00<00:00, 240.21it/s]
Epoch 418: 100%|██████████| 10/10 [00:00<00:00, 229.89it/s]
Epoch 419: 100%|██████████| 10/10 [00:00<00:00, 250.23it/s]
Epoch 420: 100%|██████████| 10/10 [00:00<00:00, 56.05it/s]
Epoch 421: 100%|██████████| 10/10 [00:00<00:00, 214.67it/s]
Epoch 422: 100%|██████████| 10/10 [00:00<00:00, 210.70it/s]
Epoch 423: 100%|██████████| 10/10 [00:00<00:00, 247.79it/s]
Epoch 424: 100%|██████████| 10/10 [00:00<00:00, 263.97it/s]
Epoch 425: 100%|██████████| 10/10 [00:00<00:00, 263.87it/s]
Epoch 426: 100%|██████████| 10/10 [00:00<00:00, 257.62it/s]
Epoch 427: 100%|██████████| 10/10 [00:00<00:00, 249.59it/s]
Epoch 428: 100%|██████████| 10/10 [00:00<00:00, 230.51it/s]
Epoch 429: 100%|██████████| 10/10 [00:00<00:00, 262.81it/s]
Epoch 430: 100%|██████████| 10/10 [00:00<00:00, 261.13it/s]
Epoch 431: 100%|██████████| 10/10 [00:00<00:00, 238.06it/s]
Epoch 432: 100%|██████████| 10/10 [00:00<00:00, 255.81it/s]
Epoch 433: 100%|██████████| 10/10 [00:00<00:00, 246.16it/s]
Epoch 434: 100%|██████████| 10/10 [00:00<00:00, 246.04it/s]
Epoch 435: 100%|██████████| 10/10 [00:00<00:00, 236.42it/s]
Epoch 436: 100%|██████████| 10/10 [00:00<00:00, 242.49it/s]
Epoch 437: 100%|██████████| 10/10 [00:00<00:00, 253.26it/s]
Epoch 438: 100%|██████████| 10/10 [00:00<00:00, 288.04it/s]
Epoch 439: 100%|██████████| 10/10 [00:00<00:00, 221.24it/s]
Epoch 440: 100%|██████████| 10/10 [00:00<00:00, 221.40it/s]
Epoch 441: 100%|██████████| 10/10 [00:00<00:00, 238.10it/s]
Epoch 442: 100%|██████████| 10/10 [00:00<00:00, 261.44it/s]
Epoch 443: 100%|██████████| 10/10 [00:00<00:00, 261.28it/s]
Epoch 444: 100%|██████████| 10/10 [00:00<00:00, 273.80it/s]
Epoch 445: 100%|██████████| 10/10 [00:00<00:00, 257.10it/s]
Epoch 446: 100%|██████████| 10/10 [00:00<00:00, 220.01it/s]
Epoch 447: 100%|██████████| 10/10 [00:00<00:00, 234.32it/s]
Epoch 448: 100%|██████████| 10/10 [00:00<00:00, 227.58it/s]
Epoch 449: 100%|██████████| 10/10 [00:00<00:00, 228.44it/s]
Epoch 450: 100%|██████████| 10/10 [00:00<00:00, 246.29it/s]
Epoch 451: 100%|██████████| 10/10 [00:00<00:00, 208.79it/s]
Epoch 452: 100%|██████████| 10/10 [00:00<00:00, 209.23it/s]
Epoch 453: 100%|██████████| 10/10 [00:00<00:00, 243.98it/s]
Epoch 454: 100%|██████████| 10/10 [00:00<00:00, 263.06it/s]
Epoch 455: 100%|██████████| 10/10 [00:00<00:00, 219.99it/s]
Epoch 456: 100%|██████████| 10/10 [00:00<00:00, 226.50it/s]
Epoch 457: 100%|██████████| 10/10 [00:00<00:00, 241.28it/s]
Epoch 458: 100%|██████████| 10/10 [00:00<00:00, 255.39it/s]
Epoch 459: 100%|██████████| 10/10 [00:00<00:00, 252.95it/s]
Epoch 460: 100%|██████████| 10/10 [00:00<00:00, 232.76it/s]
Epoch 461: 100%|██████████| 10/10 [00:00<00:00, 272.16it/s]
Epoch 462: 100%|██████████| 10/10 [00:00<00:00, 254.43it/s]
Epoch 463: 100%|██████████| 10/10 [00:00<00:00, 289.31it/s]
Epoch 464: 100%|██████████| 10/10 [00:00<00:00, 289.61it/s]
Epoch 465: 100%|██████████| 10/10 [00:00<00:00, 262.23it/s]
Epoch 466: 100%|██████████| 10/10 [00:00<00:00, 270.83it/s]
Epoch 467: 100%|██████████| 10/10 [00:00<00:00, 280.01it/s]
Epoch 468: 100%|██████████| 10/10 [00:00<00:00, 249.79it/s]
Epoch 469: 100%|██████████| 10/10 [00:00<00:00, 251.83it/s]
Epoch 470: 100%|██████████| 10/10 [00:00<00:00, 265.33it/s]
Epoch 471: 100%|██████████| 10/10 [00:00<00:00, 279.89it/s]
Epoch 472: 100%|██████████| 10/10 [00:00<00:00, 244.09it/s]
Epoch 473: 100%|██████████| 10/10 [00:00<00:00, 258.85it/s]
Epoch 474: 100%|██████████| 10/10 [00:00<00:00, 270.12it/s]
Epoch 475: 100%|██████████| 10/10 [00:00<00:00, 58.95it/s]
Epoch 476: 100%|██████████| 10/10 [00:00<00:00, 225.41it/s]
Epoch 477: 100%|██████████| 10/10 [00:00<00:00, 175.26it/s]
Epoch 478: 100%|██████████| 10/10 [00:00<00:00, 242.70it/s]
Epoch 479: 100%|██████████| 10/10 [00:00<00:00, 244.59it/s]
Epoch 480: 100%|██████████| 10/10 [00:00<00:00, 252.09it/s]
Epoch 481: 100%|██████████| 10/10 [00:00<00:00, 258.25it/s]
Epoch 482: 100%|██████████| 10/10 [00:00<00:00, 278.41it/s]
Epoch 483: 100%|██████████| 10/10 [00:00<00:00, 242.29it/s]
Epoch 484: 100%|██████████| 10/10 [00:00<00:00, 235.16it/s]
Epoch 485: 100%|██████████| 10/10 [00:00<00:00, 225.87it/s]
Epoch 486: 100%|██████████| 10/10 [00:00<00:00, 221.56it/s]
Epoch 487: 100%|██████████| 10/10 [00:00<00:00, 243.74it/s]
Epoch 488: 100%|██████████| 10/10 [00:00<00:00, 232.47it/s]
Epoch 489: 100%|██████████| 10/10 [00:00<00:00, 271.08it/s]
Epoch 490: 100%|██████████| 10/10 [00:00<00:00, 293.77it/s]
Epoch 491: 100%|██████████| 10/10 [00:00<00:00, 249.02it/s]
Epoch 492: 100%|██████████| 10/10 [00:00<00:00, 237.32it/s]
Epoch 493: 100%|██████████| 10/10 [00:00<00:00, 217.52it/s]
Epoch 494: 100%|██████████| 10/10 [00:00<00:00, 223.12it/s]
Epoch 495: 100%|██████████| 10/10 [00:00<00:00, 163.53it/s]
Epoch 496: 100%|██████████| 10/10 [00:00<00:00, 244.02it/s]
Epoch 497: 100%|██████████| 10/10 [00:00<00:00, 241.37it/s]
Epoch 498: 100%|██████████| 10/10 [00:00<00:00, 198.62it/s]
Epoch 499: 100%|██████████| 10/10 [00:00<00:00, 193.84it/s]
Epoch 500: 100%|██████████| 10/10 [00:00<00:00, 206.22it/s]
In [ ]:
import matplotlib.pyplot as plt
In [ ]:
# Plot loss and accuracy

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_losses, label='Training Loss', color='b')
plt.plot(val_losses, label='Validation Loss', color='orange')
plt.xlabel('Step')
plt.ylabel('Loss')
plt.title('Training and Validation Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(train_accuracies, label='Training Accuracy', color='r')
plt.plot(val_accuracies, label='Validation Accuracy', color='g')
plt.xlabel('Step')
plt.ylabel('Accuracy (%)')
plt.title('Training and Validation Accuracy')
plt.legend()
plt.tight_layout()
plt.show()
No description has been provided for this image

Testing and Visualization¶

In [ ]:
from sklearn.metrics import confusion_matrix, classification_report, roc_curve, auc
In [ ]:
# Start the timer
start_time = time.time()
# After training, evaluate on the test set
model.eval()
correct = 0
total = 0
predictions = []
with torch.no_grad():
    # get inputs from test data loader
    for df,targets in test_loader:
        # forward pass + get predicted outputs
        outputs = model(df.to(device)).squeeze()
        predicted = (outputs > 0.5).float().cpu()
        predictions.extend(predicted)
        total += targets.size(0)
        correct += (predicted == targets.cpu()).sum().item()
        
# Convert the list of predictions to a numpy array
predictions = np.array(predictions)

# calculate accuracy of predictions
test_accuracy = 100 * correct / total

# Calculate the test time
test_time = time.time() - start_time

print(f'Final Test Accuracy: {test_accuracy:.2f}%')
Final Test Accuracy: 94.62%

Confusion Matrix¶

[TP FN]
[FP TN]

In [ ]:
confusion_matrix = confusion_matrix(test_labels, predictions)
print("Confusion Matrix:")
print(confusion_matrix)
Confusion Matrix:
[[108   5]
 [  9 138]]

Classification Report¶

Female: 0, Male: 1

  • Precision: The proportion of true positive predictions among all positive predictions.
  • Recall: The proportion of true positive predictions among all actual positive instances.
  • F1-score: The harmonic mean of precision and recall, providing a balance between the two.
  • Support: The number of actual occurrences of the class in the specified dataset.
  • Accuracy: The overall accuracy of the model, which is the proportion of correct predictions among all predictions.
  • Macro average: The unweighted mean of precision, recall, and F1-score across all classes. Each class contributes equally to this metric.
  • Weighted average: The weighted mean of precision, recall, and F1-score across all classes, where each class's contribution is weighted by its support (the number of true instances).
In [ ]:
# Calculate classification report
print("Classification Report:")
print(classification_report(test_labels, predictions))
Classification Report:
              precision    recall  f1-score   support

           0       0.92      0.96      0.94       113
           1       0.97      0.94      0.95       147

    accuracy                           0.95       260
   macro avg       0.94      0.95      0.95       260
weighted avg       0.95      0.95      0.95       260

ROC & AUC¶

In [ ]:
# Compute ROC Curve and AUC
fpr, tpr, _ = roc_curve(test_labels, predictions)
roc_auc = auc(fpr, tpr)

# Plot ROC Curve
plt.figure()
plt.plot(fpr, tpr, color='darkorange', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc='lower right')
plt.show()

print(f'AUC: {roc_auc:.2f}')
No description has been provided for this image
AUC: 0.95

Saving the Results¶

In [ ]:
# Load your CSV file into a pandas DataFrame
csv_file_path = 'Data/results.csv'
df = pd.read_csv(csv_file_path)

# Define your variables
var1 = test_accuracy
var2 = len(hidden_layers)
var3 = hidden_layers
var4 = num_epochs
var5 = batch_size
var6 = learning_rate
var7 = loss_function.__class__.__name__
var8 = optimiser.__class__.__name__
var9 = momentum
var10 = alpha
var11 = weight_decay
var12 = training_time
var13 = test_time

# Create a new row with the variables and image
new_row = [var1, var2, var3, var4, var5, var6, var7, var8, var9, var10, var11, var12, var13]

# Add the new row to the DataFrame
df.loc[len(df)] = new_row

# Save the updated DataFrame back to CSV
df.to_csv(csv_file_path, index=False)

print("Row added successfully!")
Row added successfully!

Utilizing the Model¶

After training and evaluation, use the model

In [ ]:
# Specify the values for a fruit fly and get the estimated 'Sex'.
data = np.array([[0,        # Species
                  20,       # Temperature
                  1.145,    # Thorax_length
                  1.782,    # l2
                  0.605,    # l3p
                  1.490,    # l3d
                  2.096,    # lpd
                  2.095,    # l3
                  0.934,    # w1
                  1.300,    # w2
                  1.055,    # w3
                  1.83]])   # wing_loading
In [ ]:
# Convert the DataFrame to a PyTorch tensor
test_tensor = torch.tensor(data, dtype=torch.float32)

model.eval()
with torch.no_grad():
    # forward pass
    output = model(test_tensor.to(device)).squeeze()
    predicted = (output > 0.5).float().cpu()

# Convert predicted 0 or 1 back to Female or Male
decoded_class = Label_encoder_classifier.inverse_transform(np.array([int(predicted.item())]))

# Calculate certainty 
if decoded_class.item() == 'female':
    certainty = (1 - output.item()) * 100
else:
    certainty = output.item() * 100

# Print the results
print(f"Your Drosophila's (Fruit Fly's) sex is: %s, with certainty: %.2f%%" % (decoded_class.item(), certainty))
Your Drosophila's (Fruit Fly's) sex is: male, with certainty: 95.33%